package com.opal.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opal.system.entity.BdCustomerForm;
import com.opal.system.entity.ClockInQueryForm;
import com.opal.system.entity.table.*;
import com.opal.system.mapper.oracle.*;
import com.opal.system.service.StoredProcedureService;
import com.opal.utils.PageVOUtils;
import com.opal.vo.PageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.opal.system.service.HRService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class HRServiceImpl implements HRService {

    @Autowired
    private OrgSalesorgMapper orgSalesorgMapper;

    @Autowired
    private BdCustomerMapper bdCustomerMapper;

    @Autowired
    private OrgHrorgMapper orgHrorgMapper;

    @Autowired
    private TbmPeriodMapper tbmPeriodMapper;

    @Autowired
    private StoredProcedureService storedProcedureService;

    @Autowired
    private TbmImportdataMapper tbmImportdataMapper;


    @Override
    public List<OrgSalesorg> orgSalesorgList() {

        return orgSalesorgMapper.findAll();
    }

    @Override
    public List<BdCustomer> findByNamePage(BdCustomerForm bdCustomerForm, int page, int rows) {
        bdCustomerForm.setFirstIndex((page - 1) * rows);
        bdCustomerForm.setLastIndex(page * rows);
        return bdCustomerMapper.findByNamePage(bdCustomerForm);
    }

    @Override
    public int findTotal(BdCustomerForm bdCustomer) {
        return bdCustomerMapper.findTotal(bdCustomer);
    }

    @Override
    public List<OrgHrorg> orgHrorgFindAll() {
        return orgHrorgMapper.findAll();
    }

    @Override
    public Map finBySealflag(OrgHrorg orgHrorg) {
        //获取用户名字
        Subject subject = SecurityUtils.getSubject();
        OpalUser user = (OpalUser) subject.getPrincipal();
        Map msg = new HashMap();
        if (orgHrorg.getTime()==null||orgHrorg.getTime()==""){
            msg.put("msg", "请选择日期！");
            return msg;
        }
        String year = orgHrorg.getTime().substring(0, 4);
        String month = orgHrorg.getTime().substring(5, 7);
        String result = tbmPeriodMapper.finBySealflag(orgHrorg.getPkHrorg(), year, month);
        if (result == null || result.length()<0) {
            msg.put("msg", "没有相关的数据！");
            return msg;
        }
        if (user == null){
            msg.put("statu",404);
            return msg;
        }
        if (result.equals("Y")) {
            msg.put("msg", "已经封存，不允许再修改数据!");
            return msg;
        } else if (result.equals("N")) {
            int result1 = storedProcedureService.CallDelDuplicate(orgHrorg);
            if (result1 == 0) {
                msg.put("msg", "重复数据为零不需要清除！");
            } else {
                msg.put("msg", "重复考勤数据清除完成！共清除"+result1 + " 条记录");
                log.info("用户: " + user.getUserName() + "  执行了 “" + orgHrorg.getTime() + "”  的重复考勤数据清除， 共清除" + result1 + "条记录");
            }
            return msg;
        }
        msg.put("msg", "没有数据");
        return msg;
    }

    @Override
    public PageVO<TbmImportData> findByClockInInfo(ClockInQueryForm clockInQueryForm,int page,int rows) {
        clockInQueryForm.setFirstIndex((page - 1) * rows);
        clockInQueryForm.setLastIndex(page * rows);
        int count = tbmImportdataMapper.getCoumt(clockInQueryForm);
        List<TbmImportData> tbmImportDataList = tbmImportdataMapper.findByID(clockInQueryForm);
        return PageVOUtils.success(count,tbmImportDataList);

    }

    @Override
    @Transactional("oracleTransactionManager")
    public Map delectClockInInfo(List<String> id, String userName) {
        //获取用户名字
        Subject subject = SecurityUtils.getSubject();
        OpalUser user = (OpalUser) subject.getPrincipal();

//        String delectID = id.replaceAll(",", "|");
        Map msg = new HashMap();
        if (user==null){
            msg.put("msg","登录过期，正在为您转回登录界面！");
            msg.put("statu",404);
            return msg;
        }
        int total = tbmImportdataMapper.deleteByID(id);
        if (total > 0) {
            msg.put("msg", "删除了 " + userName + " 的 " + total + " 条原始打卡时间记录" + id);
            log.info("用户: "+user.getUserName() + " 删除了 " + userName + " 的 " + total + " 条原始打卡时间记录。ID为:(" + id + ")");
            return msg;
        }
        msg.put("msg", "没有数据");
        return msg;
    }

    @Override
    public int getCoumt(ClockInQueryForm clockInQueryForm) {
        return tbmImportdataMapper.getCoumt(clockInQueryForm);
    }

}
